package com.example.serviceproduct.controller;

import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.example.product.Product;
import com.example.serviceproduct.service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @GetMapping("/{orderId}")
    public List<Product> getProductsByOrderId(@PathVariable Long orderId) throws InterruptedException {
        TimeUnit.SECONDS.sleep(60);
        // 这里应该是根据orderId查询产品列表的业务逻辑
        // 示例数据 - 实际项目中应该从数据库查询
        Product product1 = new Product();
        product1.setId(1L);
        product1.setProductName("商品1");  // 修改为 productName
        product1.setPrice(new BigDecimal("100.00"));
        product1.setNum(2);

        Product product2 = new Product();
        product2.setId(2L);
        product2.setProductName("商品2");  // 修改为 productName
        product2.setPrice(new BigDecimal("200.00"));
        product2.setNum(1);

        System.out.println("getProductsByOrderId执行成功！");
        return Arrays.asList(product1, product2);
    }

    // 需要请求头的版本。（接收请求SpringMVC控制层接口和发送请求的OpenFeign客户端可以一模一样）
    @GetMapping("/secure/{orderId}")
    public List<Product> getProductsSecure(@PathVariable Long orderId, @RequestHeader("X-Auth-Token") String authToken) throws InterruptedException {
//        TimeUnit.SECONDS.sleep(62);

        if(1==1){
            throw new InterruptedException();
        }
        // 打印接收到的请求头
        System.out.println("Auth Token: " + authToken);

        // 这里应该是根据orderId查询产品列表的业务逻辑
        Product product1 = new Product();
        product1.setId(1L);
        product1.setProductName("商品1");
        product1.setPrice(new BigDecimal("100.00"));
        product1.setNum(2);

        Product product2 = new Product();
        product2.setId(2L);
        product2.setProductName("商品2");
        product2.setPrice(new BigDecimal("200.00"));
        product2.setNum(1);

        System.out.println("getProductsSecure执行成功！");
        return Arrays.asList(product1, product2);
    }

    @Autowired
    private ProductService productService;
    // 测试sentinel
    @GetMapping("/testSentinel")
    public String testSentinel() throws SystemBlockException {
        return productService.testReturnString();
    }

    @GetMapping("/test")
    public String test(HttpServletRequest request){
        // 获取所有请求头
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String, String> headers = new HashMap<>();

        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }
        log.info("请求头信息: {}", headers);
        return "test执行成功";
    }
}
